//
//  OSLoginViewController.m
//  OSChina
//
//  Created by 林涛 on 15/1/6.
//  Copyright (c) 2015年 林涛. All rights reserved.
//

#import "OSLoginViewController.h"
#import "OSNetworkRequest.h"
#import "OSUser.h"
@interface OSLoginViewController ()
@property (strong, nonatomic) IBOutlet UITextField *userNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation OSLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    OSUser *user = [[OSUser alloc]init];
    self.userNameTextField.text = user.userName;
    self.passwordTextField.text = user.password;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)loginAction:(id)sender {
    [OSNetworkRequest loginWithUserName:self.userNameTextField.text password:self.passwordTextField.text success:^(id result) {
        NSDictionary *dic = result[@"result"];
        NSLog(@"%@",result);
        
        if ([dic[@"errorCode"] isEqualToString:@"1"]) {
            NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:result];
            [mutableDic setObject:self.userNameTextField.text forKey:@"userName"];
            [mutableDic setObject:self.passwordTextField.text forKey:@"password"];
            [OSUser saveUser:mutableDic];
            [self performSegueWithIdentifier:@"LoginSegue" sender:sender];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"红薯提示" message:dic[@"errorMessage"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
        
    } failure:^(NSError *error) {
        
    }];
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
